package com.zb.frame.modules.sys.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zb.frame.base.model.MPPage;
import com.zb.frame.base.model.R;
import com.zb.frame.modules.common.controller.AbstractController;
import com.zb.frame.modules.monitor.enums.LogModule;
import com.zb.frame.modules.monitor.enums.LogType;
import com.zb.frame.modules.monitor.service.LogService;
import com.zb.frame.modules.sys.entity.ParamList;
import com.zb.frame.modules.sys.entity.ParamType;
import com.zb.frame.modules.sys.entity.Role;
import com.zb.frame.modules.sys.service.ParamListService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bangZZ
 * @since 2020-09-07
 */
@RestController
@RequestMapping("/sys/param")
public class ParamController extends AbstractController {

    @Autowired
    ParamListService paramListService;

    @Autowired
    LogService logService;

    @ApiOperation(value = "getParams", notes = "获取所有参数信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页数", dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "一页显示数量", dataType = "Integer"),
            @ApiImplicitParam(name = "name", value = "参数类型名称/别名", dataType = "String")
    })
    @GetMapping("/list")
    @RequiresPermissions("sys:param:view")
    public R getParams(Integer page, Integer limit, String keyword, Long id) {
        Integer currPage = (page == null) ? 1 : page;
        Integer pageSize = (limit == null) ? 10 : limit;
        Page<ParamList> pageInfo = new Page<>(currPage, pageSize);
        Map<String, Object> query = new HashMap<>();
        query.put("id", id);
        query.put("name", keyword);
        MPPage retData = paramListService.getParams(pageInfo, query);
        return R.ok(retData);
    }

    @ApiOperation(value = "getParam", notes = "根据参数Id获取该参数信息")
    @ApiImplicitParam(name = "id", value = "参数类型Id", dataType = "Long")
    @GetMapping("/info/{id}")
    @RequiresPermissions("sys:param:view")
    public R getParam(@PathVariable Long id) {
        ParamList param = paramListService.getById(id);
        return (param != null) ? R.ok(param) : R.error(404, "","失败，参数类型不存在！");
    }

    @ApiOperation(value = "newParam", notes = "新增参数")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "paKey", value = "参数键", dataType = "String"),
        @ApiImplicitParam(name = "paDesc", value = "参数描述", dataType = "String"),
        @ApiImplicitParam(name = "paValue", value = "参数值", dataType = "String"),
        @ApiImplicitParam(name = "typeId", value = "参数类型Id", dataType = "Long")
    })
    @PostMapping("/info")
    @RequiresPermissions("sys:param:add")
    public R newParam(@RequestBody ParamList data) {
        String paramKey = data.getPaKey();
        boolean isExist = paramListService.isExist(paramKey);
        if (isExist)
            return R.error("失败，参数Key已存在!");
        ParamList param = paramListService.addParam(data);
        if (param != null) {
            // 添加日志
            String newValue = JSON.toJSONString(param);
            logService.addLog(LogModule.SYSPARAM, LogType.ADD,"", newValue, String.valueOf(param.getPaId()), this.getUserId(),"新增参数[" + param.getPaKey() + "]成功");
            return R.ok("成功！",param.getPaId());
        } else {
            return R.error("失败!");
        }
    }

    @ApiOperation(value = "updateParam", notes = "修改参数类型")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "paDesc", value = "参数描述", dataType = "String"),
        @ApiImplicitParam(name = "paValue", value = "参数值", dataType = "String")
    })
    @PutMapping("/info/{id}")
    @RequiresPermissions("sys:param:edit")
    public R updateRole(@PathVariable String id, @RequestBody ParamList data) {
        String oldValue = JSON.toJSONString(data);
        ParamList newParam = paramListService.updateParam(data);
        if (newParam == null)
            return R.error("失败,参数不存在！");
        String newValue = JSON.toJSONString(newParam);
        // 添加日志
        logService.addLog(LogModule.SYSPARAM, LogType.UPDATE, oldValue, newValue, String.valueOf(newParam.getPaId()), this.getUserId(), "修改参数[" + newParam.getPaKey()+ "]成功");
        return R.ok("成功！");
    }

    @ApiOperation(value = "deleteParam", notes = "删除参数")
    @ApiImplicitParam(name = "paId", value = "参数主键", dataType = "Long")
    @DeleteMapping("/info/{id}")
    @RequiresPermissions("sys:param:del")
    public R deleteParam(@PathVariable Long id){
        ParamList param = paramListService.getById(id);
        if (param == null) {
            return R.error(404, "","失败，参数不存在！");
        }
        if (param.getIsSys() == 1) {
            return R.error("失败，系统默认参数不可删除！");
        }
        paramListService.removeById(param.getPaId());
        // 添加日志
        String oldValue = JSON.toJSONString(param);
        logService.addLog(LogModule.SYSPARAM, LogType.DEL, oldValue, "", String.valueOf(param.getPaId()) , this.getUserId(), "删除参数[" + param.getPaKey()+ "]成功");
        return R.ok("成功！");
    }

    @ApiOperation(value = "batchDeleteParam", notes = "批量删除参数")
    @ApiImplicitParam(name = "ids", value = "参数主键", dataType = "List")
    @PutMapping("/batch/del")
    @RequiresPermissions("sys:param:del")
    public R batchDeleteParam(@RequestBody Map data){
        List ids = (List) data.get("ids");
        boolean f = paramListService.removeByIds(ids);
        return R.ok("成功！");
    }
}
